/**
 * 
 */
package com.firebird.oms.service.sm.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.firebird.base.domain.KeyLabel;
import com.firebird.base.domain.Page;
import com.firebird.oms.dao.sm.TismAreaDao;
import com.firebird.oms.dao.sm.TismCompanyDao;
import com.firebird.oms.dao.sm.TismDepartDao;
import com.firebird.oms.domain.sm.TismArea;
import com.firebird.oms.domain.sm.TismCompany;
import com.firebird.oms.service.sm.CompanyService;


/**
 * @author qiuxj
 * @date 2009-11-22
 *
 */
public class CompanyServiceImpl implements CompanyService {
	
	private TismCompanyDao smCompanyDao;
	private TismDepartDao smDepartDao;
	private TismAreaDao smAreaDao;
	
	/**
	 * 创建责任区域
	 * @param smArea
	 */
	public void createArea(TismArea smArea) {
		smAreaDao.create(smArea);
	}
	
	/**
	 * 更新责任区域
	 * @param smArea
	 */
	public void updateArea(TismArea smArea) {
		smAreaDao.update(smArea);
	}
	
	/**
	 * 获取责任区域
	 * @param areaId
	 * @return
	 */
	public TismArea getArea(int areaId) {
		return smAreaDao.get(areaId);
	}
	
	/**
	 * 获取责任区域列表
	 * @param filter
	 * @return
	 */
	public List<TismArea> findArea(Map<String, String> filter) {
		return smAreaDao.find(filter);
	}
	
	/**
	 * 获取分页责任区域列表
	 * @param filterMap
	 * @param likeMap
	 * @param sortMap
	 * @param pageNo
	 * @param pageSize
	 * @return
	 */
	public Page pageArea(Map<String, String> filterMap, Map<String, String> likeMap, Map<String, String> sortMap,
			int pageNo, int pageSize) {
		return smAreaDao.pagedQuery(filterMap, likeMap, sortMap, pageNo, pageSize);
	}
	
	/**
	 * 创建分公司信息
	 * @param smCompany
	 */
	public void createCompany(TismCompany smCompany) {
		smCompanyDao.create(smCompany);
	}
	
	/**
	 * 修改分公司信息
	 * @param smCompany
	 */
	public void updateCompany(TismCompany smCompany) {
		smCompanyDao.update(smCompany);
	}
	
	/**
	 * 根据分公司ID获取分公司信息
	 * @param companyId
	 * @return
	 */
	public TismCompany getCompany(int companyId) {
		return smCompanyDao.getUniqueBy("companyId", companyId);
	}
	
	/**
	 * 根据filter查询分公司信息
	 * @param filter
	 * @return
	 */
	public List<TismCompany> findCompany(Map<String, Object> filter) {
		return smCompanyDao.find(filter);
	}
	
	/**
	 * 获取分公司的下级分公司
	 * @param companyId
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public List<TismCompany> getChildCompany(int companyId) {
		String hql = "from TismCompany where upperCompanyId = ? and state = 1 order by companyId";
		return smCompanyDao.find(hql, new Object[]{companyId});
	}
	
	/**
	 * 获取所有有效地分公司列表
	 * @return
	 */
	public List<KeyLabel> getCompanyKls() {
		Map<String, Object> filter = new HashMap<String, Object>();
		filter.put("state", 1);
		List<TismCompany> companys = this.findCompany(filter);
		
		List<KeyLabel> keyLabels = new ArrayList<KeyLabel>(0);
		for(TismCompany company : companys){
			keyLabels.add(new KeyLabel(company.getCompanyId().toString(), company.getCompanyName().toString()));
		}
		return keyLabels;
	}
	
	public void setSmCompanyDao(TismCompanyDao smCompanyDao) {
		this.smCompanyDao = smCompanyDao;
	}
	public void setSmDepartDao(TismDepartDao smDepartDao) {
		this.smDepartDao = smDepartDao;
	}

	public void setSmAreaDao(TismAreaDao smAreaDao) {
		this.smAreaDao = smAreaDao;
	}

}
